home *** CD-ROM | disk | FTP | other *** search
- { gfile.pas -- Demonstrate File dialogs }
-
- program GFile;
-
- {$R gfile.res}
-
- uses WinTypes, WinProcs, WObjects, WinDOS, Strings, StdDlgs;
-
- const
-
- id_Menu = 100; { Menu resource ID }
-
- noFileStr = '[unnamed]'; { No file open window title }
-
- cm_FileOpen = 101; { File-menu command IDs }
- cm_FileClose = 102;
- cm_FileSave = 103;
- cm_FileSaveAs = 104;
- cm_FileExit = 105;
-
- type
-
- GFileApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PGFileWindow = ^GFileWindow;
- GFileWindow = object(TWindow)
- FileIsOpen, FileIsChanged: Boolean;
- FileName: array[0 .. fsPathName] of Char;
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- procedure Error(S: String);
- function OpenFile: Boolean;
- function SaveFile: Boolean;
- procedure DisableAll;
- procedure EnableCommand(commandID: Integer);
- procedure DisableCommand(commandID: Integer);
- function CanClose: Boolean; virtual;
- procedure FileOpen(var Msg: TMessage);
- virtual cm_First + cm_FileOpen;
- procedure FileClose(var Msg: TMessage);
- virtual cm_First + cm_FileClose;
- procedure FileSave(var Msg: TMessage);
- virtual cm_First + cm_FileSave;
- procedure FileSaveAs(var Msg: TMessage);
- virtual cm_First + cm_FileSaveAs;
- procedure FileExit(var Msg: TMessage);
- virtual cm_First + cm_FileExit;
- procedure WMLButtonDown(var Msg: TMessage);
- virtual wm_First + wm_LButtonDown;
- end;
-
-
- { GFileApplication }
-
- {- Initialize GFileApplication object's window }
- procedure GFileApplication.InitMainWindow;
- begin
- MainWindow := New(PGFileWindow, Init(nil, noFileStr))
- end;
-
-
- { GFileWindow }
-
- {- Construct GFileWindow object }
- constructor GFileWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- FileIsOpen := false;
- FileIsChanged := false;
- DisableAll;
- EnableCommand(cm_FileOpen)
- end;
-
- {- Disable all File-menu commands }
- procedure GFileWindow.DisableAll;
- begin
- DisableCommand(cm_FileOpen);
- DisableCommand(cm_FileClose);
- DisableCommand(cm_FileSave);
- DisableCommand(cm_FileSaveAs)
- end;
-
- {- Enable menu commands }
- procedure GFileWindow.EnableCommand(commandID: Integer);
- begin
- EnableMenuItem(Attr.Menu, commandID, mf_ByCommand or mf_Enabled)
- end;
-
- {- Disable menu commands }
- procedure GFileWindow.DisableCommand(commandID: Integer);
- begin
- EnableMenuItem(Attr.Menu, commandID, mf_ByCommand or mf_Grayed)
- end;
-
- {- Display error message dialog }
- procedure GFileWindow.Error(S: String);
- var
- P: array[0 .. 255] of Char;
- begin
- MessageBox(HWindow, StrPCopy(P, S), 'Error!', mb_IconExclamation)
- end;
-
- {- Return true if application can safely end }
- function GFileWindow.CanClose: Boolean;
- var
- Answer: Integer;
- begin
- CanClose := true;
- if FileIsChanged then
- begin
- Answer := MessageBox(HWindow, 'Save changes before quitting?',
- 'Please answer', mb_YesNoCancel or mb_IconQuestion);
- if Answer = idYes then
- CanClose := SaveFile
- else if Answer = idCancel then
- CanClose := false
- end
- end;
-
- {- Perform file open and return true if successful }
- function GFileWindow.OpenFile: Boolean;
- var
- F: File;
- begin
- Assign(F, StrPas(FileName));
- {$I-} Reset(F); {$I+}
- if IoResult <> 0 then
- Error('File ' + StrPas(FileName) + ' not found')
- else begin
- { Read file information into memory here }
- Close(F); { Note: File does NOT remain open }
- FileIsOpen := true; { i.e. file information is in memory }
- FileIsChanged := false { No changes made to file as of yet }
- end;
- OpenFile := FileIsOpen { Return result to caller }
- end;
-
- {- Perform file save and return true if successful }
- { Note: Simulated; no data is written to disk }
- function GFileWindow.SaveFile: Boolean;
- var
- Answer: Integer;
- begin
- { Write in-memory data to FileName here }
- Answer := MessageBox(HWindow, 'Save file successfully?',
- 'Simulated Operation', mb_YesNo);
- if Answer = idYes then
- begin
- SaveFile := true;
- FileIsChanged := false;
- end else
- begin
- SaveFile := false;
- Error('File ' + StrPas(FileName) + ' not saved')
- end
- end;
-
- { Menu commands }
-
- {- File:Open }
- procedure GFileWindow.FileOpen(var Msg: TMessage);
- begin
- StrCopy(FileName, '*.*');
- if Application^.ExecDialog(New(PFileDialog, Init(@Self,
- PChar(sd_FileOpen), FileName))) = id_Ok then
- if OpenFile then
- begin
- SetWindowText(HWindow, FileName);
- DisableAll;
- EnableCommand(cm_FileClose);
- EnableCommand(cm_FileSaveAs)
- end
- end;
-
- {- File:Close }
- procedure GFileWindow.FileClose(var Msg: TMessage);
- var
- OkToClose: Boolean;
- Answer: Integer;
- begin
- OkToClose := true;
- if FileIsChanged then
- begin
- Answer := MessageBox(HWindow, 'Save changes before closing?',
- 'Please answer', mb_YesNoCancel or mb_IconQuestion);
- if Answer = idYes then
- OkToClose := SaveFile
- else if Answer = idCancel then
- OkToClose := false
- end;
- if OkToClose then
- begin
- FileIsOpen := false;
- FileIsChanged := false;
- SetWindowText(HWindow, noFileStr);
- DisableAll;
- EnableCommand(cm_FileOpen);
- end
- end;
-
- {- File:Save }
- procedure GFileWindow.FileSave(var Msg: TMessage);
- begin
- if SaveFile then
- DisableCommand(cm_FileSave)
- end;
-
- {- File:SaveAs }
- procedure GFileWindow.FileSaveAs(var Msg: TMessage);
- begin
- if Application^.ExecDialog(New(PFileDialog, Init(@Self,
- PChar(sd_FileSave), FileName))) = id_Ok then
- if SaveFile then
- begin
- SetWindowText(HWindow, FileName);
- DisableCommand(cm_FileSave)
- end
- end;
-
- {- File:Exit }
- procedure GFileWindow.FileExit(var Msg: TMessage);
- begin
- CloseWindow
- end;
-
- {- Simulate file edit--Click left mouse button }
- procedure GFileWindow.WMLButtonDown(var Msg: TMessage);
- begin
- if FileIsOpen then
- begin
- FileIsChanged := true;
- EnableCommand(cm_FileSave);
- MessageBox(HWindow, 'File Change Simulated',
- 'Simulated Operation', mb_Ok);
- end
- end;
-
- var
-
- GFileApp: GFileApplication;
-
- begin
- GFileApp.Init('GFileApp');
- GFileApp.Run;
- GFileApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 3/19/1991
- ---------------------------------------------------------------}
-